package com.lingyue.commons.zlib;

import com.lingyue.commons.enums.ResultCodeEnum;
import com.lingyue.commons.exceptions.CommRuntimeException;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream;

/**
 * Created by IntelliJ IDEA.
 *
 * @Description: TODO
 * @Author: BaiJinhua
 * @Date: 2019/12/26 19:40
 * @See: com.lingyue.commons.zlib
 * @Version: 1.0
 * Copyright: Copyright (c) 2019
 */
@Slf4j
public class ZlibUtils {

    /**
     * zlib压缩
     */

    public static byte[] compress(byte[] data) {
        byte[] output;
        Deflater compress = new Deflater();
        compress.reset();
        try {
            compress.setInput(data);
            compress.finish();
            @Cleanup
            ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
            byte[] buf = new byte[1024];
            while (!compress.finished()) {
                int i = compress.deflate(buf);
                bos.write(buf, 0, i);
            }
            output = bos.toByteArray();
        } catch (Exception e) {
            log.error("压缩数据异常！异常信息---->{}", e.getMessage());
            e.printStackTrace();
            throw new CommRuntimeException(ResultCodeEnum.COMPRESS_INVALID);
        }
        compress.end();
        return output;
    }

    /**
     * zlib解压
     */

    public static byte[] decompress(byte[] data) {
        byte[] output;
        Inflater decompress = new Inflater();
        decompress.reset();
        try {
            decompress.setInput(data);
            @Cleanup
            ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
            byte[] buf = new byte[1024];
            while (!decompress.finished()) {
                int i = decompress.inflate(buf);
                o.write(buf, 0, i);
            }
            output = o.toByteArray();
        } catch (Exception e) {
            log.error("解压缩数据异常！异常信息---->{}", e.getMessage());
            e.printStackTrace();
            throw new CommRuntimeException(ResultCodeEnum.DECOMPRESS_INVALID);
        }
        decompress.end();
        return output;
    }

    public static void main(String[] args) {
        String test = "{\n" +
                " \"batch_id\": \"2018111001\",\n" +
                " \"total\": 2,\n" +
                " \"batch_span_id\": \"181001\",\n" +
                " \"span_count\": 1,\n" +
                " \"finish_flag\": 0,\n" +
                " \"list\": [{\n" +
                "  \"unique_key\": \"c0001\",\n" +
                "  \"vin\": \"WBAKR0100G0S95850\",\n" +
                "  \"dealer_id\": \"30933\",\n" +
                "  \"handover_dealer_id\": \"30933\",\n" +
                "  \"brand\": \"宝马\",\n" +
                "  \"vehicle_model\": \"X3\",\n" +
                "  \"series\": \"series\",\n" +
                "  \"license_plate\": \"123\",\n" +
                "  \"vehicle_mileage\": \"100\",\n" +
                "  \"owner_contact\": \"201953330\",\n" +
                "  \"after_sales_contact\": \"201953330\",\n" +
                "  \"both_contact\": \"201953330\",\n" +
                "  \"last_billing_date\": \"2019-12-31\",\n" +
                "  \"handover_date\": \"2019-12-31\",\n" +
                "  \"insurance_start_date\": \"2019-12-31\",\n" +
                "  \"insurance_end_date\": \"2019-12-31\",\n" +
                "  \"last_workshop_visit_date\": \"2019-12-31\",\n" +
                "  \"store_contact\": \"8618501967089\",\n" +
                "  \"last_workshop_visit_spending\": \"100\",\n" +
                "  \"customer_value_afslyt\": \"哈哈\",\n" +
                "  \"customer_value_total_spending\": \"300\",\n" +
                "  \"warranty_start_date\": \"2019-12-31\",\n" +
                "  \"last_workshop_mileage\": \"200\",\n" +
                "  \"last_visit_dealer\": \"哈哈\",\n" +
                "  \"last_oil_change_date\": \"2019-12-31\",\n" +
                "  \"last_oil_change_dealer\": \"haha\",\n" +
                "  \"last_oil_change_mileage\": \"66\",\n" +
                "  \"last_visit_order_type\": \"asdf\",\n" +
                "  \"last_visit_spending\": \"30\",\n" +
                "  \"estimated_daily_avg_mileage\": \"23\",\n" +
                "  \"value_service\": \"asqw\",\n" +
                "  \"connect_drive_status\": \"哈哈\",\n" +
                "  \"last_oil_change_dealer_id\": \"123456\",\n" +
                "  \"last_visit_dealer_id\": \"23456\",\n" +
                "  \"regular_customer\": \"哈哈\",\n" +
                "  \"warranty_end_date\": \"2019-12-31\",\n" +
                "  \"first_oil_change_date\": \"2019-12-31\",\n" +
                "  \"connect_drive_flag\": \"嘿嘿\",\n" +
                "  \"bsi\": \"bsi\",\n" +
                "  \"renewal_date\": \"2019-12-31\",\n" +
                "  \"business_partner_type\": \"tepy\",\n" +
                "  \"conn_drive_end_date\": \"2019-12-31\",\n" +
                "  \"reserve_1\": \"\",\n" +
                "  \"reserve_2\": \"\",\n" +
                "  \"reserve_3\": \"\",\n" +
                "  \"reserve_4\": \"\",\n" +
                "  \"reserve_5\": \"\",\n" +
                "  \"reserve_6\": \"\",\n" +
                "  \"reserve_7\": \"\",\n" +
                "  \"reserve_8\": \"\",\n" +
                "  \"reserve_9\": \"\",\n" +
                "  \"reserve_10\": \"\"\n" +
                " }]\n" +
                "}";
        try {
            System.out.println("原长度"+test.length());
            byte[] compress = compress(test.getBytes("UTF-8"));
            String s = new String(compress);
            System.out.println(s);
            System.out.println("压缩后长度"+ s.length());
            System.out.println(new String(decompress(compress)));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
